<%@ page language="java" import="java.util.*"
	contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/context/mytags.jsp"%>
<!DOCTYPE html>
<html>
<head>
<title></title>
<t:base type="jquery,easyui,tools,DatePicker"></t:base>
<style>
	.toolbar{
		text-align: center;
		margin:10px;
	}
</style>
</head>
<body>
<div class="panel-header">
	<div class="panel-title">实验室预约申请</div>
</div>
	<t:formvalid formid="formobj" dialog="true" layout="table" enctype="multipart/form-data" 
		action="" tiptype="3">
		<input type="hidden" id="id" value=''>
 		<table cellpadding="0" cellspacing="1" class="formtable">
 			 <tr>
				<td align="right">
						<label class="Validform_label">
							班级:
						</label>
				</td>
				<td class="value"> 
                	     <input id="gradeId" name="gradeId" type="hidden"  value='${task.grade.id }'>               
					     <input id="className"  type="text" style="width: 150px" class="inputxt" 
					      readonly="readonly"   >
					     <t:choose hiddenName="gradeId" hiddenid="id" url="dictGradeController.do?goSelect" name="deviceGradeList"
                          icon="icon-search" title="选择" textname="className" isclear="false" isInit="true"  ></t:choose>
							
						<span class="Validform_checktip"></span>
						<label class="Validform_label" style="display: none;">班级</label>
						
				</td>
					
			</tr>
 		 
 		 	<tr>
 				<td align="right">
 					<label class="Validform_label"> 课程名称: </label>
				</td>
				<td class="value">
					<input id="lessonId" name="lessonId" type="hidden"  >               
					<input id="lessonName"  type="text" style="width: 150px" class="inputxt" 
					      readonly="readonly"   >
					<t:choose hiddenName="lessonId" hiddenid="id" url="lessonInfoController.do?goSelect" name="lessonList"
                          icon="icon-search" title="选择" textname="lessonName" isclear="false" isInit="true"  ></t:choose>
					<span class="Validform_checktip"></span> 
					<label class="Validform_label"	style="display: none;"> 课程名称</label>
				</td>
 			</tr>
 		 
 			<tr>
				<td align="right">
						<label class="Validform_label">
							实验项目:
						</label>
				</td>
				<td class="value"> 
                	    <input id="teachItemId" name="teachItemId" type="hidden" >               
					     <input id="itemName"  type="text" style="width: 150px" class="inputxt" 
					      readonly="readonly"  >
					     <t:choose hiddenName="teachItemId" hiddenid="id" url="dictTeachItemController.do?goSelect" name="groupList"
                          icon="icon-search" title="选择" textname="itemName" isclear="false" isInit="true"  ></t:choose> 
							
						<span class="Validform_checktip"></span>
						<label class="Validform_label" style="display: none;">实验项目</label>
						
				</td>
					
			</tr>
 			
 		
 		 
 			<tr>
				<td align="right">
						<label class="Validform_label">
							任课教师:
						</label>
				</td>
				<td class="value"> 
                	     <input id="teacherId" name="teacherId" type="hidden" >               
					     <input id="realName"  type="text" style="width: 150px" class="inputxt" 
					      readonly="readonly"   >
					     <t:choose hiddenName="teacherId" hiddenid="id" url="teacherController.do?goSelect" name="teacherList"
                          icon="icon-search" title="选择" textname="realName" isclear="false" isInit="true"  ></t:choose>
							
						<span class="Validform_checktip"></span>
						<label class="Validform_label" style="display: none;">任课教师</label>
						
				</td>
					
			</tr>
			
 			<tr>
				<td align="right">
						<label class="Validform_label">
							辅导教师:
						</label>
				</td>
				<td class="value"> 
                	     <input id="coacherId" name="coacherId" type="hidden"  >               
					     <input id="coacherName"  type="text" style="width: 150px" class="inputxt" 
					      readonly="readonly"   >
					     <t:choose hiddenName="teacherId" hiddenid="id" url="teacherController.do?goSelect" name="teacherList"
                          icon="icon-search" title="选择" textname="realName"  inputTextname="coacherName" isclear="false" isInit="true"  ></t:choose>
							
						<span class="Validform_checktip"></span>
						<label class="Validform_label" style="display: none;">辅导教师</label>
						
				</td>
					
			</tr>
			<tr>
				<td align="right">
						<label class="Validform_label">
							实验室:
						</label>
				</td>
				<td class="value"> 
                	     <input id="labId" name="labId" type="hidden" >               
					     <input id="labName"  type="text" style="width: 150px" class="inputxt" 
					      readonly="readonly" >
					     <t:choose hiddenName="labId" hiddenid="id" url="labBranchController.do?goSelect" name="labBranchList"
                          icon="icon-search" title="选择" textname="name" inputTextname="labName" isclear="false" isInit="true"  ></t:choose>
							
						<span class="Validform_checktip"></span>
						<label class="Validform_label" style="display: none;">实验室</label> 
				</td> 
			</tr>
			<tr>
	 				<td align="right">
	 					<label class="Validform_label"> 周期: </label>
					</td>
					<td class="value">
						<t:CommonSelect url="semesterInfoController.do?weekSelect"
						   name="startWeek" textField="desc" idField="number"></t:CommonSelect>--
						   
						<t:CommonSelect url="semesterInfoController.do?weekSelect"
						   name="endWeek" textField="desc" idField="number"></t:CommonSelect>
						   
						 <label><input type="radio" value="1" name="weekType" checked="checked">全周</label>
						 <label><input type="radio" value="2" name="weekType">单周</label>
						 <label><input type="radio" value="3" name="weekType">双周</label>
						 
					</td>
 			</tr>
 			<tr>
	 				<td align="right">
	 					<label class="Validform_label"> 星期: </label>
					</td>
					<td class="value">
						 <label><input type="checkbox" value="2" name="week" checked="checked">星期一</label>
						 <label><input type="checkbox" value="3" name="week" checked="checked">星期二</label>
						 <label><input type="checkbox" value="4" name="week" checked="checked">星期三</label>
						 <label><input type="checkbox" value="5" name="week" checked="checked">星期四</label>
						 <label><input type="checkbox" value="6" name="week" checked="checked">星期五</label>
						 <label><input type="checkbox" value="7" name="week">星期六</label>
						 <label><input type="checkbox" value="1" name="week">星期日</label>
					</td>
 			</tr>
 			<tr>
	 				<td align="right">
	 					<label class="Validform_label"> 上课时间: </label>
					</td>
					<td class="value">
						  <t:CommonSelect url="semesterInfoController.do?lessonOrderSelect"
						   name="startLesson" textField="desc" idField="number"></t:CommonSelect>--
						   
						<t:CommonSelect url="semesterInfoController.do?lessonOrderSelect"
						   name="endLesson" textField="desc" idField="number"></t:CommonSelect>
						 
					</td>
 			</tr> 
		</table> 
	</t:formvalid>
	
	<div class="toolbar">
		<a class='myBtn roundBorder' id="saveBtn1" onclick="addRuleBtnClicked()">保存排课信息</a>
		<div style="width:100px;display: inline-block;"></div> 
		<a class='myBtn roundBorder' onclick="save(this)">提交排课信息</a>
	</div>
	
	<table class="commonTable" id='appointTable'>
		<thead>
			<tr>
				<th>上课时间</th><th>实验室</th><th>教师</th><th>操作</th>
			</tr>
		</thead> 
		<tbody> 
		</tbody>
	</table>  
	
</body>
<script>
	var rules = new Array();
	var index = 0;
	
	function addRuleBtnClicked(){
		var id = $('#id').val();
		if(id == '' || id== null){
			addRule();
		}else{
			modRule(id);
		}
	}
	
	//添加规则
	function addRule(){ 
		if($('#saveBtn1').hasClass("disable")) return;

		var week = [];
		var rule = {
				id:index,
				gradeId:$('#gradeId').val(),
				className:$('#className').val(),
				teachItemId:$('#teachItemId').val(),
				itemName:$('#itemName').val(),
				teacherId:$('#teacherId').val(),
				teacherName:$('#realName').val(),
				labId:$('#labId').val(),
				labName:$('#labName').val(),
				weekType:$('input[name="weekType"]:checked').val(),
				week:week,
				startLesson:$('#startLesson').val(),
				endLesson:$('#endLesson').val(),
				startWeek:$('#startWeek').val(),
				endWeek:$('#endWeek').val(),
				coacherId:$('#coacherId').val(),
				coacherName:$('#coacherName').val(),
				lessonId:$('#lessonId').val(),
				lessonName:$('#lessonName').val()
				
		};
		
		$('input[name="week"]:checked').each(function(){
			week.push({day:$(this).val()});
		});
		
	 
		var valid = isValid(rule);
		if(!valid) return; 
		
		rules[index] = rule;
		showRuleInTable(index);
		clearInputPanel();
		index++; 
	}
	
	function modRule(id){
		if($('#saveBtn1').hasClass("disable")) return;

		var week = [];
		var rule = {
				id:id,
				gradeId:$('#gradeId').val(),
				className:$('#className').val(),
				teachItemId:$('#teachItemId').val(),
				itemName:$('#itemName').val(),
				teacherId:$('#teacherId').val(),
				teacherName:$('#realName').val(),
				labId:$('#labId').val(),
				labName:$('#labName').val(),
				weekType:$('input[name="weekType"]:checked').val(),
				week:week,
				startLesson:$('#startLesson').val(),
				endLesson:$('#endLesson').val(),
				startWeek:$('#startWeek').val(),
				endWeek:$('#endWeek').val(),
				coacherId:$('#coacherId').val(),
				coacherName:$('#coacherName').val(),
				lessonId:$('#lessonId').val(),
				lessonName:$('#lessonName').val()
				
		};
		
		$('input[name="week"]:checked').each(function(){
			week.push({day:$(this).val()});
		});
		
		var valid = isValid(rule);
		if(!valid) return;
		
		rules[id] = rule;
		showRuleInTable(index);
		clearInputPanel();
	}
	
	function showRuleInTable(i){
		var tr = $('#tr'+i);
		if(tr.length > 0){
			showTrValue(tr,i);
		}else{
			$('#appointTable tbody').append("<tr id='tr"+i+"'></tr>");
			tr = $('#tr'+i);
			showTrValue(tr,i);
		}
	}
	
	function isValid(rule){
		 if(rule.gradeId == '' || rule.gradeId == null){
				tip('请选择班级');
				return false;
			}
			if(rule.teachItemId == '' || rule.teachItemId == null){
				tip('请选择实验项目');
				return false;
			}
			if(rule.teacherId == '' || rule.teacherId == null){
				tip('请选择教师');
				return false;
			}
			if(rule.labId == '' || rule.labId == null){
				tip('请选择实验室');
				return false;
			}  
			if(rule.lessonId == '' || rule.lessonId == null){
				tip('请选择课程');
				return false;
			} 
			if(rule.startLesson > rule.endLesson){
				tip('开始上课时间不能早于结束时间');
				return false;
			}
			if(rule.week.length == 0){
				tip('星期不能为空！');
				return false;
			}
			if(rule.startWeek > rule.endWeek){
				tip('开始周不能早于结束周！');
				return false;
			}
		return true;
	}
	
	function showTrValue(trObj,i){
		 
		var rule = rules[i];
		var td = "<td>第"+rule.startWeek+"周-第"+rule.endWeek+"周<br>第"+rule.startLesson+"节-第"+rule.endLesson+"节</td>";
		td += "<td>"+rule.labName+"</td>";
		td += "<td>"+rule.teacherName+"</td>";
		td += ("<td><a href='javascript:void(0)' onclick='deleteItem("+i+")'>删除</a>&nbsp;");
		td += "<a href='javascript:void(0)' onclick='modItem("+i+")'>修改</a>";
		td += "</td>";
		trObj.html(td);
	}
	
	function clearInputPanel(){
		$('#gradeId').val('');
		$('#className').val('');
		$('#teachItemId').val('');
		$('#itemName').val('');
		$('#teacherId').val('');
		$('#realName').val('');
		$('#labId').val('');
		$('#labName').val('');
		$('#id').val('');
		$('#coacherId').val('');
		$('#coacherName').val('');
		$('#lessonId').val('');
		$('#lessonName').val('');
		
	}
	
	function modItem(i){
		
		if($('#saveBtn1').hasClass("disable")) return;
		
		var rule = rules[i];
		$('#gradeId').val(rule.gradeId);
		$('#className').val(rule.className);
		$('#teachItemId').val(rule.teachItemId);
		$('#itemName').val(rule.itemName);
		$('#teacherId').val(rule.teacherId);
		$('#realName').val(rule.teacherName);
		$('#labId').val(rule.labId);
		$('#labName').val(rule.labName);
 		$('#week').val(rule.week);
		$('#startLesson').val(rule.startLesson);
		$('#endLesson').val(rule.endLesson);
		$('#startWeek').val(rule.startWeek);
		$('#endWeek').val(rule.endWeek) 
		$('#id').val(i);
		$('#coacherId').val(rule.coacherId);
		$('#coacherName').val(rule.coacherName);
		$('#lessonId').val(rule.lessonId);
		$('#lessonName').val(rule.lessonName);
 
		
		$('input[name="weekType"]').each(function(){
			if($(this).val() == rule.weekType){
				this.checked=true;
			} 
		});
		
		var week = rule.week;
		$('input[name="week"]').each(function(){
			var isCheck = false;
			for(var j=0;j<week.length;j++){
				var val = week[j].day;
				if($(this).val() == val){
					isCheck = true;
				}
			}
			
			this.checked = isCheck;
		});
		
	}
	
	function deleteItem(i){
		rules[i] = null;
		$('#tr'+i).remove();
	}
	
	function save(obj){
		if(rules.length == 0){
			tip('无排课信息');
			return;
		}
		
		if($(obj).hasClass("disable")){
			tip('数据正在提交，请耐心等待');
			return;
		}
		
		$(obj).addClass("disable");
		$('#saveBtn1').addClass("disable");
		$(obj).text("数据提交中，请等待！")
		
		$.ajax({
			url:'teacherAppointController.do?save',
			type:"POST",
			data:"data="+JSON.stringify(rules),
			success:function(data){
				var data = $.parseJSON(data);
				window.top.tip(data.msg);
				
				if(data.success){
					// 转到课表信息
					window.location.href="labAppointApplyController.do?list&readonly";
				}else{
					$(obj).removeClass("disable");
					$(obj).text("提交排课信息");
					$('#saveBtn1').removeClass("disable");
				}
			}
		});
	}

</script>
 <style>
 	.toolbar a.disable,
 	.toolbar a.disable:hover{
 		cursor: not allowed;
 		background-color: rgb(246,246,246);
 		color:black;
 	}
 </style>
   
 